﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.dal;
namespace bzxxxt.admin.service
{
    public class DepartmentService : BaseService
    {
        public string getall()
        {
            var deplist = (from b in DB.tbDepartment
                           where b.parentId == 0
                           orderby b.sort
                           select new DepartmentEntity
                           {
                               id = b.id,
                               text = b.name,
                               sort = b.sort,
                               code = b.code,
                               parentId = b.parentId,
                               addDate = b.addDate
                           }).ToList();

            deplist.ForEach(mu =>
            {
                var children = (from b in DB.tbDepartment
                                where b.parentId == mu.id
                                select new DepartmentEntity
                                {
                                    id = b.id,
                                    text = b.name,
                                    sort = b.sort,
                                    code = b.code,
                                    parentId = b.parentId,
                                    addDate = b.addDate
                                }).ToList();
                if (children != null)
                {
                    mu.children = new List<DepartmentEntity>();
                    mu.children.AddRange(children);
                }
            });
            string json = JsonUtil.ToJson(deplist.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where)
        {
            int totalCount = 0;
            where = where + " and parentId=0";
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.tbDepartment.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("tbDepartment", "*", order, where, pageSize, pageIndex, paramTotalCount)).ToList();
            var deplist = (from b in pager
                           select new DepartmentEntity
                           {
                               id = b.id,
                               text = b.name,
                               sort = b.sort,
                               code = b.code,
                               parentId = b.parentId,
                               addDate = b.addDate
                           }).ToList();

            deplist.ForEach(mu =>
            {
                var children = (from b in DB.tbDepartment
                                where b.parentId == mu.id
                                select new DepartmentEntity
                                {
                                    id = b.id,
                                    text = b.name,
                                    sort = b.sort,
                                    code = b.code,
                                    parentId = b.parentId,
                                    addDate = b.addDate
                                }).ToList();
                if (children != null)
                {
                    mu.children = new List<DepartmentEntity>();
                    mu.children.AddRange(children);
                }
            });
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = deplist });
            return json;
        }

        public string add(tbDepartment b)
        {
            DB.tbDepartment.Add(b);
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string edit(tbDepartment b)
        {
            var old = DB.tbDepartment.FirstOrDefault(m => m.id == b.id);
            old.name = b.name;
            old.parentId = b.parentId;
            old.sort = b.sort;
            old.code = b.code;
            old.parentId = b.parentId;
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string delete(string strIds)
        {
            if (string.IsNullOrEmpty(strIds))
            {
                return JsonUtil.ToJson(new { success = false, msg = "没有选择要删除的记录！" });
            }
            string[] arr = strIds.Split(',');
            List<int> ids = new List<int>();
            foreach (var id in arr)
            {
                ids.Add(int.Parse(id));
            }
            //删除当前及子部门
            var depts = DB.tbDepartment.Where(m => ids.Contains(m.id) || ids.Contains(m.parentId)).ToList();
            if (depts != null)
            {
                depts.ForEach(m => DB.tbDepartment.Remove(m));
                DB.SaveChanges();
                return JsonUtil.ToJson(new { success = true });
            }
            else
            {
                return JsonUtil.ToJson(new { success = false, msg = "删除失败！" });
            }

        }
    }
}